import requests
import json
import pymongo

# mongo连接
mongo_conn = pymongo.MongoClient()

for page in range(1, 231 + 1):
    # 模拟请求
    url = "https://careers.tencent.com/tencentcareer/api/post/Query"
    params = {'timestamp': '1729559801526', 'countryId': '', 'cityId': '', 'bgIds': '', 'productId': '',
              'categoryId': '', 'parentCategoryId': '', 'attrId': '', 'keyword': '', 'pageIndex': page, 'pageSize': '10',
              'language': 'zh-cn', 'area': 'cn'}
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    response = requests.get(url, params=params, headers=headers)
    result = response.content.decode()
    print(response.status_code)
    print(result)

    # 数据解析 json反序列化解析需要的数据字段
    result = json.loads(result)
    # 批量插入 参数要求为list套dict
    if not result.get('Data').get('Posts'):
        break
    mongo_conn.tengxun.data.insert_many(result.get('Data').get('Posts'))

    # for item in result.get('data').get('list'):
    #     order = item.get('order')
    #     title = item.get('title')
    #     print(page,order, title)
    #     # 保存到mongo 数据存储 单条插入  参数要求为dicts
    #     mongo_conn.bilibili.data.insert_one({'order': order, 'title': title})
